package pl.mchaniewski.ryneczek.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.mchaniewski.ryneczek.dao.QuotationDao;
import pl.mchaniewski.ryneczek.pojo.Quotation;
import pl.mchaniewski.ryneczek.utilities.DateUtil;

@Repository("quotationDao")
@Transactional(readOnly = true)
public class HibernateQuotationDao extends
        HibernateGenericDao<Quotation, Integer> implements QuotationDao {

    @Override
    @Transactional(readOnly = false)
    public void remove(Quotation quotation) {
        Quotation tempQuotation = getEntityManager().getReference(
                Quotation.class, quotation.getQuotationid());
        getEntityManager().remove(tempQuotation);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findLatest(boolean wasInLastUpdate) {
        String queryString = "from Quotation q where q.company.wasInLastUpdate=:wasInLastUpdate and q.listingDate = (select max(subq.listingDate) from Quotation subq where subq.company = q.company) group by q.company order by q.company.name asc";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("wasInLastUpdate", Boolean.valueOf(wasInLastUpdate));

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findLatest(Integer companyid) {
        String queryString = "from Quotation q where q.company.companyid=:id and q.listingDate = (select max(subq.listingDate) from Quotation subq where subq.company = q.company) group by q.company";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("id", companyid);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findLatest(Integer companyid, boolean wasInLastUpdate) {
        String queryString = "from Quotation q where q.company.wasInLastUpdate=:wasInLastUpdate and q.company.companyid=:id and q.listingDate = (select max(subq.listingDate) from Quotation subq where subq.company = q.company) group by q.company";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("id", companyid);
        query.setParameter("wasInLastUpdate", Boolean.valueOf(wasInLastUpdate));

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findLatestByDate(Integer companyid, Date date) {
        Date from = DateUtil.setTime(date, 0, 0, 0);
        Date to = DateUtil.setTime(date, 23, 59, 59);

        String queryString = "from Quotation q where q.company.companyid=:companyid and q.listingDate = (select max(subq.listingDate) from Quotation subq where subq.company = q.company and subq.listingDate>=:fromDate and subq.listingDate<=:toDate) group by q.company";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("companyid", companyid);
        query.setParameter("fromDate", from);
        query.setParameter("toDate", to);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findByDateAndCompanyId(Integer companyid, Date date) {
        Date from = DateUtil.setTime(date, 0, 0, 0);
        Date to = DateUtil.setTime(date, 23, 59, 59);

        String queryString = "from Quotation q where q.company.companyid = :companyid and q.listingDate>=:fromDate and q.listingDate<=:toDate";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("companyid", companyid);
        query.setParameter("fromDate", from);
        query.setParameter("toDate", to);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Quotation> findByDateTimeAndCompanyId(Integer companyid,
            Date date) {
        Date from = DateUtil.setTime(date, 0, 0, 0);

        String queryString = "from Quotation q where q.company.companyid = :companyid and q.listingDate>=:fromDate and q.listingDate<:toDate";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("companyid", companyid);
        query.setParameter("fromDate", from);
        query.setParameter("toDate", date);

        return query.getResultList();
    }

}
